package com.li.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.exception.BaseException;
import com.li.mapper.RoleDeptMapper;
import com.li.pojo.dto.RoleDto;
import com.li.pojo.dto.RoleDeptDTO;
import com.li.pojo.dto.RolePageDto;
import com.li.pojo.po.PageResult;
import com.li.pojo.po.Role;
import com.li.mapper.RoleMapper;
import com.li.pojo.po.RoleDept;
import com.li.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.tomcat.util.buf.StringUtils;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-13
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleDeptMapper roleDeptMapper;

    @Override
    public void addRole(RoleDto roleDto) {

        try {
            Role role = new Role();
            BeanUtils.copyProperties(roleDto,role);
            role.setCreateTime(LocalDateTime.now().toString());
            role.setUpdateTime(LocalDateTime.now().toString());
            role.setMenuIds(StringUtils.join(roleDto.getMenuIds()));
//        role.setDeptIds(StringUtils.join(roleDto.getDeptIds()));
//        role.setDeptIds(roleDto.getDeptIds());
            roleMapper.insert(role);
        } catch (Exception e) {
            throw new BaseException("权限添加过多，请重新添加");
        }
    }

    @Override
    public PageResult findByPage(RolePageDto rolePageDto) {
        Page<Role> page = PageHelper.startPage(rolePageDto.getPageNum(), rolePageDto.getPageSize());

        roleMapper.findByPage(rolePageDto);

        return new PageResult(200,"查询成功",null,page.getResult(),page.getTotal());
    }

    @Override
    public Role findById(Integer roleId) {
        Role role = roleMapper.selectById(roleId);
        return role;
    }

    @Override
    public void deleteByIds(List<Integer> roleIds) {
        roleMapper.deleteBatchIds(roleIds);
    }



    @Override
    public void updateRole(RoleDto roleDto) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDto, role);
//        role.setMenuIds(StringUtils.join(roleDto.getMenuIds()));
//        role.setDeptIds(StringUtils.join(roleDto.getDeptIds()));
        role.setUpdateTime(LocalDateTime.now().toString());
        roleMapper.updateRole(role);
    }

    @Override
    public void dataScope(String s) {
        try {
            JSONObject jsonObject = new JSONObject(s);
            String dataScope = String.valueOf(jsonObject.get("dataScope"));
            Integer roleId = (Integer) jsonObject.get("roleId");
            String deptIds = String.valueOf(jsonObject.get("deptIds"));
            deptIds = deptIds.replace("[", "").replace("]", "");
            Role role = new Role();
            role.setRoleId(roleId);
            role.setDataScope(dataScope);
            role.setDeptIds(deptIds);
            role.setUpdateTime(LocalDateTime.now().toString());
            roleMapper.updateRole(role);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Role> getAll() {
        return roleMapper.getAll();
    }

}
